package com.aube.plugin.rank.model;

import com.huyn.baseframework.model.AppShare;
import com.huyn.baseframework.model.Category;
import com.huyn.baseframework.utils.StringUtils;

import java.util.Iterator;
import java.util.LinkedHashMap;

/**
 * Created by huyaonan on 16/8/11.
 */
public class RankAppShare extends AppShare {

    public int width;
    public int height;
    public String shareLogoUrl;

    public String weiboPicUrl;
    public String weiboTitle;
    public String weiboContent;

    public boolean isShareLogoValid() {
        return StringUtils.isNotBlank(shareLogoUrl) && width > 0 && height > 0;
    }

    public static RankAppShare setupShareData(Category category) {
        if(category == null || category.modelExtra == null)
            return null;

        try {
            LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) category.modelExtra;
            if(lhm.containsKey("shareInfo")) {
                LinkedHashMap<String, Object> newLhm = (LinkedHashMap<String, Object>) ((LinkedHashMap<String, Object>) category.modelExtra).get("shareInfo");
                
                if(newLhm != null) {
                    Iterator<String> keyIterator = newLhm.keySet().iterator();
                    
                    RankAppShare share = new RankAppShare();

                    while (keyIterator.hasNext()) {
                        String key = keyIterator.next();
                        Object value = newLhm.get(key);

                        if("width".equalsIgnoreCase(key)) {
                            share.width = (int) ((double) value);
                        } else if("height".equalsIgnoreCase(key)) {
                            share.height = (int) ((double) value);
                        } else if("shareLogoUrl".equalsIgnoreCase(key)) {
                            share.shareLogoUrl = (String) value;
                        } else if("picurl".equalsIgnoreCase(key)) {
                            share.picurl = (String) value;
                        } else if("url".equalsIgnoreCase(key)) {
                            share.url = (String) value;
                        } else if("title".equalsIgnoreCase(key)) {
                            share.title = (String) value;
                        } else if("content".equalsIgnoreCase(key)) {
                            share.content = (String) value;
                        } else if("weiboPicUrl".equalsIgnoreCase(key)) {
                            share.weiboPicUrl = (String) value;
                        } else if("weiboTitle".equalsIgnoreCase(key)) {
                            share.weiboTitle = (String) value;
                        } else if("weiboContent".equalsIgnoreCase(key)) {
                            share.weiboContent = (String) value;
                        }
                    }

                    return share;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
